﻿using MyShop;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 经典项目某某管理系统.Erp
{
    public partial class FrmGoodView : Form
    {
        public FrmGoodView()
        {
            InitializeComponent();
        }
        //绑定商品类别
        private void BindType()
        {
            DataTable dt = new DataTable();
            string sql = "select * from Type";
            DBHelper.PrepareSql(sql);
            dt = DBHelper.ExecQuery();
            DataRow dr = dt.NewRow();
            dr["TypeName"] = "请选择";
            dr["TypeID"] = "0";
            dt.Rows.InsertAt(dr, 0);
            this.cmbClass.DataSource = dt;
            this.cmbClass.ValueMember = "TypeID";

            this.cmbClass.DisplayMember = "TypeName";

        }
        private void BindData()
        {
            string sql = "select * from Goods inner join [Type] on Goods.TypeID = [Type].TypeID where 1 = 1 ";
            if (!this.textBox1.Text.Equals(""))
            {
                sql += " and BarCode ='" + this.textBox1.Text + "'";
            }
            if (!this.textBox2.Text.Equals(""))
            {
                sql += " and GoodsName like '%" + this.textBox2.Text + "%'";
            }
            if (!this.cmbDate.Text.Equals("全部"))
            {
                sql += " and StockDate between '"
                    + this.dateStart.Value.ToString("yyyy-MM-dd") + "' and '"
                    + this.dateEnd.Value.ToString("yyyy-MM-dd") + "'";
            }
            if (!this.cmbClass.Text.Equals("请选择"))
            {
                sql+=" and Goods.TypeID = " + this.cmbClass.SelectedValue;
            }
            DBHelper.PrepareSql(sql);
            DataTable dt = DBHelper.ExecQuery();
            this.dataGridView1.DataSource = dt;
            this.label7.Text ="记录行数"+this.dataGridView1.Rows.Count.ToString();
        }
        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void FrmGoodView_Load(object sender, EventArgs e)
        {
            //// TODO: 这行代码将数据加载到表“testDBDataSet.Type”中。您可以根据需要移动或移除它。
            this.cmbDate.SelectedIndex = 0;
            BindType();
            BindData();
            //this.typeTableAdapter.Fill(this.testDBDataSet.Type);
       
        }



        private void cmbDate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cmbDate.Text.Equals("全部"))
            {
                this.dateStart.Enabled = false;
                this.dateEnd.Enabled = false;
            }
            if (this.cmbDate.Text.Equals("本年"))
            {
                this.dateStart.Enabled = true;
                this.dateEnd.Enabled = true;
                DateTime date = DateTime.Now;
                int dateOfYear = date.DayOfYear;
                this.dateStart.Value = date.AddDays(-dateOfYear + 1);
                this.dateEnd.Value = date.AddDays(-dateOfYear + 1).AddYears(1).AddDays(-1);
            }
            if (this.cmbDate.Text.Equals("本月"))
            {
                this.dateStart.Enabled = true;
                this.dateEnd.Enabled = true;
                DateTime date = DateTime.Now;
                int dateOfMonth = date.Day;
                this.dateStart.Value = date.AddDays(-dateOfMonth + 1);
                //加一个月的第一个天 再加下一个月的负一天 就得出本月最后一天
                this.dateEnd.Value = date.AddDays(-dateOfMonth + 1).AddMonths(1).AddDays(-1);

            }
            if (this.cmbDate.Text.Equals("本周"))
            {
                this.dateStart.Enabled = true;
                this.dateEnd.Enabled = true;
                DateTime date = DateTime.Now;
                int dayOfWeek = Convert.ToInt32(date.DayOfWeek);
                if (dayOfWeek == 0)
                {
                    dayOfWeek = 7;
                }
                this.dateStart.Value = date.AddDays(-dayOfWeek + 1);
                this.dateEnd.Value = date.AddDays(7 - dayOfWeek);

            }
            if (this.cmbDate.Text.Equals("本日"))
            {
                this.dateStart.Enabled = true;
                this.dateEnd.Enabled = true;
                DateTime dateTime = DateTime.Now;
                this.dateStart.Value = dateTime;
                this.dateEnd.Value = dateTime;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            BindData();
        }
    }
}
